package com.lzy.crm.recovery;

import java.util.Date;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.annotation.Resource;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import com.lzy.crm.base.date.DateUtil;
import com.lzy.crm.domain.TradeFlow;
import com.lzy.crm.domain.User;
import com.lzy.crm.service.TradeFlowService;
import com.lzy.crm.util.XmlUtil;
import com.lzy.crm.wechat.consts.WechatPayProperties;
import com.lzy.crm.wechat.util.HttpClientCustomSSL;
import com.lzy.crm.wechat.util.HttpUtils;
import com.lzy.crm.wechat.util.PayUtil;
import com.lzy.crm.wechat.util.RandomUtil;
import com.lzy.crm.wechat.util.XMLUtils;


/**
 * 对账接口
 * @author Administrator
 *
 */
@Component
public class TradeRecoveryJob {

	private static final Logger logger = LogManager.getLogger(TradeRecoveryJob.class);
	
	private static final String TRANSFERS_QUERY = "https://api.mch.weixin.qq.com/mmpaysptrans/query_bank "; // 企业付款
	
	
	@Resource
	TradeFlowService tradeFlowService;
	
	
	public void work() {
		System.out.println("---------------------同步begin---------------------"+DateUtil.getCurrentLongTimeChina());
		try {
			
			Date date = DateUtil.getNextDay(new Date());
			String startDate = DateUtil.formatDateWithoutTime(date)+" 00:00:00";
			String endDate = DateUtil.formatDateWithoutTime(date)+" 23:59:59"; 
			List<TradeFlow> tradeFlowList = tradeFlowService.getTradeFlowByStatus("1", startDate, endDate);
			
			for(TradeFlow tradeFlow :tradeFlowList){
				//随机数
				String nonce_str1 = RandomUtil.randomString(RandomUtil.LETTER_NUMBER_CHAR, 32);// StringUtils.getStrRandom(28);
				//获取签名
				TreeMap<String, Object> parm = new TreeMap<String, Object>();
				parm.put("mch_id", WechatPayProperties.getPartnerId());
				parm.put("partner_trade_no",tradeFlow.getPartnerTradeNo() ); //商户订单号
				parm.put("nonce_str", nonce_str1);
				parm.put("sign", PayUtil.getSign (parm, WechatPayProperties.getPartnerKey()));
				
				String restxml = HttpUtils.posts(TRANSFERS_QUERY, XmlUtil.xmlFormat(parm, false));
				
				System.out.println("结果分析：restxml"+restxml);
				
			}
		} catch (Exception e) {
			logger.error(e);
		}
		System.out.println("---------------------同步end---------------------"+DateUtil.getCurrentLongTimeChina());
	}
	
	
}
